package com.tsu.mall.order.Controller;

import com.tsu.mall.common.VO.CartWaresVO;
import com.tsu.mall.common.util.RequestUtil;
import com.tsu.mall.order.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private ICartService iCartService;
    @GetMapping("/cartPage")
    public String cartPafe(Model model){
        Integer currentMemberId=RequestUtil.getCurrent().getId();
        model.addAttribute("currentMemberId",currentMemberId);
        return "cart";
    }
    @PostMapping("/addCart")
    @ResponseBody
    public void addCart(Integer waresId, Integer waresNum,String memberWaresColor,String memberWaresVersion){
         iCartService.addCart(waresId,waresNum,memberWaresColor,memberWaresVersion);
    }
    @PostMapping("/cartWaresInfo")
    @ResponseBody
    public List<CartWaresVO> currentMemberWaresInfo(Integer currentMemberId){
        return iCartService.selectCurrentMemberWaresInfo(currentMemberId);

    }

    @GetMapping("/delCartWares")
    @ResponseBody
    public void delCartWares(Integer cartId){
        iCartService.removeById(cartId);
    }

    @PostMapping("/loginOrNO")
    @ResponseBody
    public Integer loginOrNO(){
        if(RequestUtil.getCurrent()!=null){
            return 1;
        }
        return 0;
    }


    //更新数据库中的 当前会员 买的当前商品 的数量
    @PostMapping("/updateAmount")
    @ResponseBody
    public void updateAmount(Integer currentMemberId,Integer currentWaresId,Integer amount,String currentColor,String currentVersion){
        iCartService.doUpdate(currentMemberId,currentWaresId,amount,currentColor,currentVersion);
    }

}
